<?php

    class Product {

        private $ProductId;
        private $ProductType;
        private $Title;
        private $Summary;
        private $Price;
        private $Stock;

        public function __construct($id, $type, $title, $summary, $price, $stock) {
            $this->ProductId = $id;
            $this->ProductType = $type;
            $this->Title = $title;
            $this->Summary = $summary;
            $this->Price = $price;
            $this->Stock = $stock;
        }

        public function __get($key) {
            if(property_exists($this, $key)) {
                return $this->{$key};
            }
            else {
                return NULL;
            }
        }

        public function __set($key, $value) {
            throw new Exception('Class is immutable.');
        }

        public static function InsertProduct($product) {
            Connection::Connect();
            if($try = Product::ReadProduct($product->ProductId) != NULL)
                    return $try->ProductId;

            $sql = mysql_query(
                "
                    INSERT INTO 
                    Product (
                            ProductType,
                            Title,
                            Summary,
                            Price, 
                            Stock
                    ) 
                    VALUES (
                            '$product->ProductType', 
                            '$product->Title', 
                            '$product->Summary', 
                            '$product->Price', 
                            '$product->Stock'
                    )
                "
            );
            
            if($sql == FALSE)
                return NULL;

            $sql = mysql_query(
                "SELECT LAST_INSERT_ID() as 'LAST_INSERT_ID'"
            );
            
            if($sql == NULL)
                return NULL;

            $rows = mysql_num_rows($sql);
            if($rows == 1) {
                    while($row = mysql_fetch_assoc($sql))
                    {
                            return $row['LAST_INSERT_ID'];
                    }
            }

            return NULL;
        }

        public static function ReadProduct($productid) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    SELECT 
                        *
                    FROM 
                        Product 
                    WHERE
                        ProductId = '$productid' 
                        AND
                        Deleted = FALSE
                "
            );
            
            if($sql == NULL)
                return NULL;

            $rows = mysql_num_rows($sql);
            if($rows == 1) {
                while($row = mysql_fetch_assoc($sql))
                {
                    $id = $row['ProductId'];
                    $type = $row['ProductType'];
                    $title = $row['Title'];
                    $summary = $row['Summary'];
                    $price = $row['Price'];
                    $stock = $row['Stock'];
                    return new Product($id, $type, $title, $summary, $price, $stock);
                }
            }
            return NULL;
        }

        public static function UpdateProduct($product) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    UPDATE 
                        Product 
                    SET 
                        ProductType = '$product->ProductType', 
                        Title = '$product->Title', 
                        Summary = '$product->Summary', 
                        Price = '$product->Price', 
                        Stock = '$product->Stock' 
                    WHERE 
                        ProductId = '$product->ProductId'
                        AND
                        Deleted = FALSE
                " 
            );
            
            if($sql == FALSE)
                return NULL;
            
            return $product->ProductId;
        }

        public static function DeleteProduct($productid) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    UPDATE 
                        Product 
                    SET 
                        Deleted = TRUE 
                    WHERE 
                        ProductId = '$productid'
                " 
            );
            
            if($sql == FALSE)
                return FALSE;
            
            return TRUE;
        }

        public static function SearchProduct($product) {
            Connection::Connect();
            $where = "WHERE Deleted = FALSE ";
            $conds_present = TRUE;
            if($product->ProductId != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where . "ProductId = '$product->ProductId' ";
            }
            if($product->ProductType != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where . "ProductType = '$product->ProductType' ";
            }
            if($product->Title != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where . "Title LIKE '%$product->Title%' ";
            }
            if($product->Summary != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where . "Summary LIKE '%$product->Summary%' ";
            }
            if($product->Price != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where . "Price = '$product->Price' ";
            }
            if($product->Stock != NULL) {
                if($conds_present)
                    $where = $where . "AND ";
                $conds_present = TRUE;
                $where = $where . "Stock = '$product->Stock' ";
            }
            if(!$conds_present)
                $where = "";

            $sql = mysql_query(
                "
                    SELECT DISTINCT
                        *
                    FROM 
                        Product 
                "
                .
                $where
            );
            
            if($sql == NULL)
                return NULL;

            $items = array();
            while($row = mysql_fetch_assoc($sql))
            {
                $id = $row['ProductId'];
                $type = $row['ProductType'];
                $title = $row['Title'];
                $summary = $row['Summary'];
                $price = $row['Price'];
                $stock = $row['Stock'];
                $items[] = new Product($id, $type, $title, $summary, $price, $stock);
            }
            return items;
        }

    }

?>